<?php

class User_CatalogosController extends Model3_Scaffold_Controller
{

    public function __construct($request)
    {
        $this->_sc = new Scaffold_DefaultDb_Catalog();
        parent::__construct($request);
    }
    
    public function init()
    {
        if (!Model3_Auth::isAuth())
        {
            $this->redirect('Index/index');
        }
    }
    public function indexAction()
    {
        $credentials = Model3_Auth::getCredentials();
        $catalogsHelper = new Helper_Catalogs();
        $this->view->catalogs = $catalogsHelper->getCatalogsByUserId($credentials['id']);
    }

    public function  addAction()
    {
        // obtencion de los datos de la base
        $credentials = Model3_Auth::getCredentials();
        $this->view->catalogs = $this->getUserCatalogs($credentials['id']);

        if ($this->getRequest()->isPost())
        {
            $this->_post = $this->getRequest()->getPost();
            $this->_post['client'] = $credentials['id']; 
        }
        parent::addAction();
    }

    public function  editAction()
    {
        // obtencion de los datos de la base
        $credentials = Model3_Auth::getCredentials();
        $this->view->catalogs = $this->getUserCatalogs($credentials['id']);
        $catalog = null;

        $idCatalog = $this->getRequest()->getParam('id');
        if($idCatalog != null && $idCatalog != '' && is_numeric($idCatalog))
        {
            // obtenemos el catalogo
            $dbs = Model3_Registry::getInstance()->get('databases');
            $em = $dbs['DefaultDb'];
            /* @var $em Doctrine\ORM\EntityManager */
            $catalogAdapter = $em->getRepository('DefaultDb_Entities_Catalog');
            $catalog =$catalogAdapter->find($idCatalog);
        }

        if ($this->getRequest()->isPost())
        {
            $this->_post = $this->getRequest()->getPost();
            $this->_post['client'] = $credentials['id'];
        }
        $this->view->catalog = $catalog;
        parent::editAction();
    }
    
    public function deleteAction()
    {
        $idCatalog = $this->getRequest()->getParam('id');
        $em = $this->getEntityManager('DefaultDb');
        $catalogRepo = $em->getRepository('DefaultDb_Entities_Catalog');
            
        $catalog = $catalogRepo->find($idCatalog);
        $products = $catalog->getProducts();
        
      
        if(count($products) > 0)
            $this->view->products = true;
        
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            
            if($this->view->products == true) //El catalogo tiene productos que se deberan poner en un estatus de eliminados
            {
                foreach ($products as $product)
                {
                    $product->setStatus('2');
                    $em->persist($product);
                    $em->flush();
                }
            }
            if($post['delete'] == 1)
            {
                parent::deleteAction();
                $this->view->result = true;
            }
            else
                if($post['delete'] == 0)
                    $this->redirect ('User/Catalogos/index');
        }    
        
    }

    private function getUserCatalogs($userId)
    {
        $dbs = Model3_Registry::getInstance()->get('databases');
        $em = $dbs['DefaultDb'];
        /* @var $em Doctrine\ORM\EntityManager */
        $userAdapter = $em->getRepository('DefaultDb_Entities_User');
        $user =$userAdapter->find($userId);
        $catalogs = $user->getCatalogs();
        return $catalogs;
    }


}
